/*
 * CarlosCanvas.java
 *
 * Created on 29 de Junho de 2007, 10:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Carlos;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 * @author JAKE
 */
public class CarlosCanvas extends GameCanvas {
    
    //---------------------------------------------------------
    //   dimension fields
    //  (constant after initialization)
    
    /**
     * the height of the green region below the ground.
     */
    static int GROUND_HEIGHT = 32;
    
    /**
     * a screen dimension.
     */
    static int CORNER_X;
    
    /**
     * a screen dimension.
     */
    static int CORNER_Y;
    
    /**
     * a screen dimension.
     */
    static int DISP_WIDTH;
    
    /**
     * a screen dimension.
     */
    static int DISP_HEIGHT;
    
    /**
     * a font dimension.
     */
    static int FONT_HEIGHT;
    
    /**
     * the default font.
     */
    static Font FONT;
    
    /**
     * a font dimension.
     */
    static int SCORE_WIDTH;
    
    /**
     * The width of the string that displays the time,
     * saved for placement of time display.
     */
    static int TIME_WIDTH;
    
    
    //---------------------------------------------------------
    //   game object fields
    
    /**
     * a handle to the display.
     */
    Display myDisplay;
    
    
    /**
     * whether or not the game has ended.
     */
    static boolean myGameOver;
    
    /**
     * the player's score.
     */
    int myScore = 0;
    
    /**
     * How many ticks we start with.
     */
    int myInitialGameTicks = 950;
    
    /**
     * this is saved to determine if the time string needs
     * to be recomputed.
     */
    int myOldGameTicks = myInitialGameTicks;
    
    /**
     * the number of game ticks that have passed.
     */
    int myGameTicks = myOldGameTicks;
    
    /**
     * whether or not this has been painted once.
     */
    boolean myInitialized;
    
    /**
     * The initial time string.
     */
    static String myInitialString = "1:00";
    
    /**
     * we save the time string to avoid recreating it
     * unnecessarily.
     */
    String myTimeString = myInitialString;
    
    
    CarlosLayerManager layerManager;
    
    /** Creates a new instance of CarlosCanvas */
    public CarlosCanvas(Display d) {
        super(false);
        this.myDisplay=d;
        myDisplay.setCurrent(this);
        
    }
    

    
    /**
     * clears the key states.
     */
    void flushKeys() {
        getKeyStates();
    }
    
    public void pintaCarlos(){
        try {
            layerManager.checkKeys(getKeyStates());
            layerManager.execute();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        flushGraphics();
        paint(getGraphics());
    }
    
    public void paint(Graphics g) {
        // perform the calculations if necessary:
        if(!myInitialized) {
            CORNER_X = g.getClipX();
            CORNER_Y = g.getClipY();
            DISP_WIDTH = g.getClipWidth();
            DISP_HEIGHT = g.getClipHeight();
            FONT = g.getFont();
            FONT_HEIGHT = FONT.getHeight();
            SCORE_WIDTH = FONT.stringWidth("Score: 000");
            TIME_WIDTH = FONT.stringWidth("Time: " + myInitialString);
            
            myInitialized = true;
        }
        // clear the screen:
        g.setColor(0xffffff);
        g.fillRect(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT);
        g.setColor(0x0000ff00);
        g.fillRect(CORNER_X, CORNER_Y + DISP_HEIGHT - GROUND_HEIGHT,
                DISP_WIDTH, DISP_HEIGHT);
        // create (if necessary) then paint the layer manager:
        if(layerManager==null){
            layerManager=new CarlosLayerManager(CORNER_X, CORNER_Y+FONT_HEIGHT*2,
                    DISP_WIDTH, DISP_HEIGHT-FONT_HEIGHT*2-GROUND_HEIGHT);
        }
        try {
            layerManager.paint(g);
        } catch (Exception ex) {
            ex.printStackTrace();
               //errorMsg(g,ex);
        }
        
    }
    
    //-------------------------------------------------------
    //  error methods
    
    /**
     * Converts an exception to a message and displays
     * the message..
     */
    void errorMsg(Exception e) {
        errorMsg(getGraphics(), e);
        flushGraphics();
    }
    
    /**
     * Converts an exception to a message and displays
     * the message..
     */
    void errorMsg(Graphics g, Exception e) {
        if(e.getMessage() == null) {
            errorMsg(g, e.getClass().getName());
        } else {
            errorMsg(g, e.getClass().getName() + ":" + e.getMessage());
        }
    }
    
    /**
     * Displays an error message if something goes wrong.
     */
    void errorMsg(Graphics g, String msg) {
        // clear the screen
        g.setColor(0xffffff);
        g.fillRect(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT);
        int msgWidth = FONT.stringWidth(msg);
        // write the message in red
        g.setColor(0x00ff0000);
        g.setFont(FONT);
        g.drawString(msg, (DISP_WIDTH - msgWidth)/2,
                (DISP_HEIGHT - FONT_HEIGHT)/2, g.TOP|g.LEFT);
        myGameOver = true;
    }
    
    
}
